/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdDatosReporteOrden.java
 *
 * Created on 18-oct-2010, 10:43:35
 */

package Reportes;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.ExcelTableExporter;
import administracion.helper.Listas;
import java.io.File;
import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import suministros.ejb30.TO.ParametrosConsultaTO;
import suministros.ejb30.TO.TipoTO;

/**
 *
 * @author Luis_Angel
 */
public class jdDatosReporteMovimientos extends javax.swing.JDialog {
    private BindingGroup bindingGroup;
    private JTableBinding jTableBinding;
    private String titulo;

    /** Creates new form jdDatosReporteOrden */
    public jdDatosReporteMovimientos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void setDatos(ParametrosConsultaTO params,int tipo_reporte){
        TipoTO tipo=Listas.getTipo_porId(params.getTipo_orden());
        switch(tipo_reporte){
            case 1:{
                    titulo="Movimientos de Almacen";
                    actualizaTablaReporte1(params);
                    break;
                    }
            case 2:{
                    titulo="Movimientos de "+tipo.getDescripcion_valor()+" por Cuenta Contable";
                    actualizaTablaReporte2(params);
                    break;
                    }
            case 3:{
                    titulo="Movimientos de "+tipo.getDescripcion_valor()+" por Suministro";
                    actualizaTablaReporte3(params);
                    break;
                    }
            case 4:{
                    titulo="Movimientos de "+tipo.getDescripcion_valor()+" por Proveedor";
                    actualizaTablaReporte4(params);
                    break;
                    }
        }
        jlblTitulo.setText(titulo);
    }

    private void actualizaTablaReporte1(ParametrosConsultaTO params){
         this.bindingGroup = new BindingGroup();
        try {
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, GestionPedidosDelegate.getInstance().listarMovimientosTotales(params) , this.jtOrdenes);
        } catch (Exception ex) {
            Logger.getLogger(jdDatosReporteMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }

             JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${codigo}"));

            columnBinding0.setColumnName("NroMovimiento");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${codsuministro}"));

            columnBinding1.setColumnName("CodSuministro");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding4_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${dessuministro}"));

            columnBinding4_1.setColumnName("Suministro");
            columnBinding4_1.setColumnClass(String.class);
            columnBinding4_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${nroOrden}"));

            columnBinding4.setColumnName("Orden");
            columnBinding4.setColumnClass(String.class);
            columnBinding4.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${pedido}"));

            columnBinding3.setColumnName("Pedido");
            columnBinding3.setColumnClass(String.class);
            columnBinding3.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${transferencia}"));

            columnBinding3_1.setColumnName("Transferencia");
            columnBinding3_1.setColumnClass(String.class);
            columnBinding3_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding3_2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cantidad}"));

            columnBinding3_2.setColumnName("Cantidad");
            columnBinding3_2.setColumnClass(BigDecimal.class);
            columnBinding3_2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${precio}"));

            columnBinding2_1.setColumnName("Precio");
            columnBinding2_1.setColumnClass(BigDecimal.class);
            columnBinding2_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${zona}"));

            columnBinding2.setColumnName("Zona");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding5 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

            columnBinding5.setColumnName("Fecha");
            columnBinding5.setColumnClass(String.class);
            columnBinding5.setEditable(false);

             JTableBinding.ColumnBinding columnBinding20 = this.jTableBinding.addColumnBinding(ELProperty.create("${tipomoneda}"));

            columnBinding20.setColumnName("TipoMoneda");
            columnBinding20.setColumnClass(String.class);
            columnBinding20.setEditable(false);

            JTableBinding.ColumnBinding columnBinding50 = this.jTableBinding.addColumnBinding(ELProperty.create("${cambio_moneda}"));

            columnBinding50.setColumnName("Cambio");
            columnBinding50.setColumnClass(BigDecimal.class);
            columnBinding50.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


    }

     private void actualizaTablaReporte2(ParametrosConsultaTO params){
         this.bindingGroup = new BindingGroup();
        try {
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, GestionPedidosDelegate.getInstance().listarMovimientoCuentas(params) , this.jtOrdenes);
        } catch (Exception ex) {
            Logger.getLogger(jdDatosReporteMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${codigo}"));

            columnBinding0.setColumnName("NroMovimiento");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${codsuministro}"));

            columnBinding1.setColumnName("CodSuministro");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding4_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${dessuministro}"));

            columnBinding4_1.setColumnName("Suministro");
            columnBinding4_1.setColumnClass(String.class);
            columnBinding4_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${nroOrden}"));

            columnBinding4.setColumnName("Orden");
            columnBinding4.setColumnClass(String.class);
            columnBinding4.setEditable(false);


            JTableBinding.ColumnBinding columnBinding3_2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cantidad}"));

            columnBinding3_2.setColumnName("Cantidad");
            columnBinding3_2.setColumnClass(BigDecimal.class);
            columnBinding3_2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${precio}"));

            columnBinding2_1.setColumnName("Precio");
            columnBinding2_1.setColumnClass(BigDecimal.class);
            columnBinding2_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${zona}"));

            columnBinding2.setColumnName("Zona");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding5 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

            columnBinding5.setColumnName("Fecha");
            columnBinding5.setColumnClass(String.class);
            columnBinding5.setEditable(false);

             JTableBinding.ColumnBinding columnBinding6 = this.jTableBinding.addColumnBinding(ELProperty.create("${cuenta}"));

            columnBinding6.setColumnName("Cuenta");
            columnBinding6.setColumnClass(String.class);
            columnBinding6.setEditable(false);

            JTableBinding.ColumnBinding columnBinding20 = this.jTableBinding.addColumnBinding(ELProperty.create("${tipomoneda}"));

            columnBinding20.setColumnName("TipoMoneda");
            columnBinding20.setColumnClass(String.class);
            columnBinding20.setEditable(false);

            JTableBinding.ColumnBinding columnBinding50 = this.jTableBinding.addColumnBinding(ELProperty.create("${cambio_moneda}"));

            columnBinding50.setColumnName("Cambio");
            columnBinding50.setColumnClass(BigDecimal.class);
            columnBinding50.setEditable(false);


            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


    }

    private void actualizaTablaReporte3(ParametrosConsultaTO params){
         this.bindingGroup = new BindingGroup();
        try {
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, GestionPedidosDelegate.getInstance().listarMovimientosSuministros(params) , this.jtOrdenes);
        } catch (Exception ex) {
            Logger.getLogger(jdDatosReporteMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${codigo}"));

            columnBinding0.setColumnName("NroMovimiento");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${codsuministro}"));

            columnBinding1.setColumnName("CodSuministro");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding4_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${dessuministro}"));

            columnBinding4_1.setColumnName("Suministro");
            columnBinding4_1.setColumnClass(String.class);
            columnBinding4_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${nroOrden}"));

            columnBinding4.setColumnName("Orden");
            columnBinding4.setColumnClass(String.class);
            columnBinding4.setEditable(false);


            JTableBinding.ColumnBinding columnBinding3_2 = this.jTableBinding.addColumnBinding(ELProperty.create("${cantidad}"));

            columnBinding3_2.setColumnName("Cantidad");
            columnBinding3_2.setColumnClass(BigDecimal.class);
            columnBinding3_2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${precio}"));

            columnBinding2_1.setColumnName("Precio");
            columnBinding2_1.setColumnClass(BigDecimal.class);
            columnBinding2_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${zona}"));

            columnBinding2.setColumnName("Zona");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding5 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

            columnBinding5.setColumnName("Fecha");
            columnBinding5.setColumnClass(String.class);
            columnBinding5.setEditable(false);


               JTableBinding.ColumnBinding columnBinding20 = this.jTableBinding.addColumnBinding(ELProperty.create("${tipomoneda}"));

            columnBinding20.setColumnName("TipoMoneda");
            columnBinding20.setColumnClass(String.class);
            columnBinding20.setEditable(false);

            JTableBinding.ColumnBinding columnBinding50 = this.jTableBinding.addColumnBinding(ELProperty.create("${cambio_moneda}"));

            columnBinding50.setColumnName("Cambio");
            columnBinding50.setColumnClass(BigDecimal.class);
            columnBinding50.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


    }

     private void actualizaTablaReporte4(ParametrosConsultaTO params){
         this.bindingGroup = new BindingGroup();
        try {
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ, GestionPedidosDelegate.getInstance().listarMovimientosProveedores(params) , this.jtOrdenes);
        } catch (Exception ex) {
            Logger.getLogger(jdDatosReporteMovimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${codigo}"));

            columnBinding0.setColumnName("NroMovimiento");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);


            JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${nroOrden}"));

            columnBinding4.setColumnName("Orden");
            columnBinding4.setColumnClass(String.class);
            columnBinding4.setEditable(false);


            JTableBinding.ColumnBinding columnBinding3_2 = this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

            columnBinding3_2.setColumnName("Fecha");
            columnBinding3_2.setColumnClass(String.class);
            columnBinding3_2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${codProveedor}"));

            columnBinding2_1.setColumnName("Ruc");
            columnBinding2_1.setColumnClass(String.class);
            columnBinding2_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${proveedor}"));

            columnBinding2.setColumnName("Proveedor");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(false);

            JTableBinding.ColumnBinding columnBinding5 = this.jTableBinding.addColumnBinding(ELProperty.create("${zona}"));

            columnBinding5.setColumnName("Zona");
            columnBinding5.setColumnClass(String.class);
            columnBinding5.setEditable(false);

            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtOrdenes = new javax.swing.JTable();
        this.jtOrdenes.getTableHeader().setReorderingAllowed(false) ;
        jButton1 = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jlblTitulo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jtOrdenes.setAutoCreateRowSorter(true);
        jtOrdenes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtOrdenes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jtOrdenes);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 754, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE))
        );

        jButton1.setFont(new java.awt.Font("Arial", 1, 12));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/apps/xcalc.png"))); // NOI18N
        jButton1.setText("EXPORTAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jbCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CANCELAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        jlblTitulo.setFont(new java.awt.Font("Arial", 1, 14));
        jlblTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlblTitulo.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(250, 250, 250)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(218, 218, 218)
                        .addComponent(jlblTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jbCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlblTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jbCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed

        this.dispose();
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed

}//GEN-LAST:event_jbCancelarKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setDialogType(1);
        int i = jfc.showDialog(null, "Guardar Excel(XLS)");
        if (i == 1) return;
        ExcelTableExporter sd =   new ExcelTableExporter(jtOrdenes, new File(jfc.getSelectedFile().getPath() + ".xls"),"Ordenes",titulo);
        sd.export();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                jdDatosReporteMovimientos dialog = new jdDatosReporteMovimientos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JLabel jlblTitulo;
    private javax.swing.JTable jtOrdenes;
    // End of variables declaration//GEN-END:variables

}
